﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using Cloak.Reflection;

namespace Cloak.Mvp.Autofac.ViewParameters
{
	/// <summary>
	/// A check that constructor parameters named "view" appear in the first position
	/// </summary>
	public sealed class ViewParameterFirstSpecification : IViewParameterSpecification
	{
		public const string ParameterName = "view";

		/// <summary>
		/// Determines if the specified parameter is a constructor parameter named "view" in the first position
		/// </summary>
		/// <param name="parameter">The parameter to check for representing a view</param>
		/// <returns>Whether the specified parameter is a constructor parameter named "view" in the first position</returns>
		public bool IsView(ParameterInfo parameter)
		{
			var isView = false;

			var constructor = parameter.Member as ConstructorInfo;

			if(constructor != null)
			{
				var viewAttribute = parameter.GetAttribute<ViewAttribute>();

				var index = constructor.GetParameters().ToList().IndexOf(parameter);

				isView = index == 0 && parameter.Name == ParameterName && (viewAttribute == null || viewAttribute.IsView);
			}

			return isView;
		}
	}
}